package org.csu.logistics.Controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import org.csu.logistics.Service.AlipayService;
import org.csu.logistics.common.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class PayController {

    @Autowired
    @Qualifier("alipayService")
    private AlipayService alipayService;

    /**
     * web 订单支付
     */
    @PostMapping("getPagePay")
    @ResponseBody
    public Map<Object, Object> getPagePay(@RequestBody Map<String,String> data) throws Exception{
        /** 模仿数据库，从后台调数据*/
        String outTradeNo = data.get("orderId");
        Double totalAmount = Double.parseDouble(data.get("orderPrice"));
        String subject = data.get("orderName");

        String pay = alipayService.webPagePay(outTradeNo, totalAmount, subject);
        System.out.println(pay);

        Map<Object, Object> pays = new HashMap<>();
        pays.put("pay", pay);

        return pays;
    }

    /**
     * app 订单支付
     */
    @GetMapping("getAppPagePay")
    public CommonResponse<?> getAppPagePay() throws Exception{
        /** 模仿数据库，从后台调数据*/
        String outTradeNo = "131233";
        Double totalAmount = 1231.1;
        String subject = "天猫超市012";

        String pay = alipayService.appPagePay(outTradeNo, totalAmount, subject);

        Object json = JSONObject.toJSON(pay);

        System.out.println(json);

        return CommonResponse.createForSuccess(json);
    }

    /**
     * 交易查询
     */
    @PostMapping("aipayQuery")
    public CommonResponse<?> alipayQuery() throws Exception{
        /**调取支付订单号*/
        String outTradeNo = "41241249";

        String query = alipayService.query(outTradeNo);

        Object json = JSONObject.toJSON(query);

        /*JSONObject jObject = new JSONObject();
        jObject.get(query);*/
        return CommonResponse.createForSuccess(json);
    }

    /**
     * 退款
     * @throws AlipayApiException
     */
    @GetMapping("alipayRefund")
    public CommonResponse<?> alipayRefund(
            @RequestParam("outTradeNo")String outTradeNo,
            @RequestParam(value = "outRequestNo", required = false)String outRequestNo,
            @RequestParam(value = "refundAmount", required = false)Integer refundAmount
    ) throws AlipayApiException{

        /** 调取数据*/
        //String outTradeNo = "15382028806591197";
        String refundReason = "用户不想购买";
        //refundAmount = 1;
        //outRequestNo = "22";

        String refund = alipayService.refund(outTradeNo, refundReason, refundAmount, outRequestNo);

        System.out.println(refund);

        return CommonResponse.createForSuccess(refund);
    }

    /**
     * 退款查询
     * @throws AlipayApiException
     */
    @PostMapping("refundQuery")
    public CommonResponse<?> refundQuery() throws AlipayApiException{

        /** 调取数据*/
        String outTradeNo = "13123";
        String outRequestNo = "2";

        String refund = alipayService.refundQuery(outTradeNo, outRequestNo);

        return CommonResponse.createForSuccess(refund);

    }

    /**
     * 交易关闭
     * @throws AlipayApiException
     */
    @PostMapping("alipayclose")
    public CommonResponse<?> alipaycolse() throws AlipayApiException{

        /** 调取数据*/
        String outTradeNo = "13123";

        String close = alipayService.close(outTradeNo);

        return CommonResponse.createForSuccess(close);
    }

}
